package com.sx.pipeline.ui

import android.os.Handler
import android.os.Looper
import com.blankj.utilcode.util.ToastUtils
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYBaseAppBarActivity
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.databinding.FragCityPickerBinding
import com.sx.pipeline.utils.LocationUtils
import com.zaaach.citypicker.CityPicker
import com.zaaach.citypicker.adapter.OnPickListener
import com.zaaach.citypicker.model.City
import com.zaaach.citypicker.model.HotCity
import com.zaaach.citypicker.model.LocateState
import com.zaaach.citypicker.model.LocatedCity

/**
 * @Author: YY
 * @Date: 2022/8/22
 * @Desc: TODO：暂未使用，后端不支持...
 */
class CityPickerActivity: CYDataBindingBaseActivity<FragCityPickerBinding, CYBaseViewModel>(R.layout.frag_city_picker) {

    // 自定义热门城市
    private var hotCities: MutableList<HotCity> = mutableListOf()


    override fun initData() {
        super.initData()
        initCity()
    }

    private fun initCity() {
        hotCities.add(HotCity("北京", "北京", "101010100"))
        hotCities.add(HotCity("上海", "上海", "101020100"))
        hotCities.add(HotCity("广州", "广东", "101280101"))
        hotCities.add(HotCity("深圳", "广东", "101280601"))
        hotCities.add(HotCity("杭州", "浙江", "101210101"))

        //城市选择
        CityPicker.from(this@CityPickerActivity)
            .enableAnimation(true)
            //.setAnimationStyle(com.zaaach.citypicker.R.style.DefaultCityPickerAnimation)
            .setHotCities(hotCities)
            .setOnPickListener(object : OnPickListener {
                override fun onPick(position: Int, data: City) {
                    object : Thread() {
                        override fun run() {
                            try {
                                Looper.prepare() //增加部分
                                ToastUtils.showShort(data.code + data.name)
                                finish()
                                Looper.loop() //增加部分
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }
                    }.start()
                }

                override fun onLocate() {
                    //定位接口，需要APP自身实现，这里模拟一下定位
                    Handler().postDelayed(Runnable { //定位完成之后更新数据
                        CityPicker.from(this@CityPickerActivity)
                            .locateComplete(
                                LocatedCity(LocationUtils.getAddress(application).locality,
                                    LocationUtils.getAddress(application).adminArea, ""),
                                LocateState.SUCCESS
                            )
                    }, 3000)
                }

                override fun onCancel() {
                    finish()
                }
            }).show()
    }
}